<?
include 'classes/GlobalConst.php';

function getPayingStatus($pStatus) {
	if ($pStatus == GlobalConst::$PAYING_STATUS) {
		return '<img src="images/m_pay.gif" border=0 alt="Paying" title="Paying" align=absmiddle>';
	}else if ($pStatus == GlobalConst::$WATTING_STATUS) {
		return '<img src="images/m_wait.gif" border=0 alt="Waiting" title="Waiting" align=absmiddle>';
	}else if ($pStatus == GlobalConst::$PROBLEM_STATUS) {
		return '<img src="images/m_prob.gif" border=0 alt="Problem" title="Problem" align=absmiddle>';
	}else if ($pStatus == GlobalConst::$NOT_PAYING_STATUS) {
		return '<img src="images/m_npay.gif" border=0 alt="Not Paying" title="Not Paying" align=absmiddle>';
	}
}

/*echo kieu withdrawl*/
function getWithDrawalType($withdrawal_type) {
	if ($withdrawal_type == GlobalConst::$MANUAL_WITHDRAWAL) {
		echo 'Manual';
	}else if ($withdrawal_type == GlobalConst::$INSTANCE_WITHDRAWAL) {
		echo 'Instant';
	}else if($withdrawal_type == GlobalConst::$AUTOMATIC_WITHDRAWAL){
		echo 'Automatic';
	}
}

function getOurRatting($ratting ) {
	for($i = 1; $i <= 5; ++ $i) {
		if ($i <= intval ($ratting)){
			echo '<img src="images/full_star.gif" align=absmiddle>';
		}else{
			echo '<img src="images/empty_star.gif" align=absmiddle>';
		}
	}
}

$frm ['gid'] = intval ( $frm ['gid'] );
$q = 'select                hl_groups.*,               sum(hl_listings.status = 2) as cnew        from               hl_groups left outer join hl_listings               on hl_groups.id = hl_listings.group_id        group  by               hl_groups.id ';

if (! ($sth = mysql_query ( $q ))) {
	exit ( mysql_error () );
}
$listings = array ();
$listing = '';
while ( $row = mysql_fetch_array ( $sth ) ) {
	if (($row ['id'] == 1 or $frm ['gid'] == $row ['id'])) {
		$listing = $row;
	}
	array_push ( $listings, $row );
}
$q = 'select count(*) as count         from               hl_listings        where               group_id = ' . $listing ['id'] . ' and status = ' . GlobalConst::$PROGRAM_WATING_APPROVAL;
if (! ($sth = mysql_query ( $q ))) {
	exit ( mysql_error () );
}
$row = mysql_fetch_array ( $sth );
$count_all = $row ['count'];
$onpage = GlobalConst::$PRG_ON_PAGE;
$page = $frm ['p'];
$colpages = ceil ( $count_all / $onpage );
if ($page <= 1) {
	$page = 1;
}
if (($colpages < $page and 1 < $colpages)) {
	$page = $colpages;
}
$from = ($page - 1) * $onpage;
$q = 'select *,                date_format(date_added, \'%b %D, %Y\') as added,
               date_format(date_started, \'%b %D, %Y\') as started,
               date_format(date_closed, \'%b %D, %Y\') as closed        from               hl_listings        where               group_id = ' . $listing ['id'] . ('' . ' and               status = 2        order by id        limit ' . $from . ', ' . $onpage . ' ');
if (! ($sth = mysql_query ( $q ))) {
	exit ( mysql_error () );
}
$listings = array ();
while ( $row = mysql_fetch_array ( $sth ) ) {
	$votes_summary = array ('0' => 0, '1' => 0, '2' => 0, '3' => 0 );
	$votes_all = 0;
	$avg_vote = '0.0';
	$q = 'select count(*) as cvotes, vote from hl_votes where listing_id = ' . $row ['id'] . ' and confirm = \'0\' group by vote';
	if (! ($ssth = mysql_query ( $q ))) {
		exit ( mysql_error () );
	}
	while ( $srow = mysql_fetch_array ( $ssth ) ) {
		$votes_all += $srow ['cvotes'];
		$votes_summary [$srow ['vote']] = $srow ['cvotes'];
	}
	if (0 < $votes_all) {
		$avg_vote = sprintf ( '%.01f', ($votes_summary ['3'] * 10 + $votes_summary ['2'] * 5 - $votes_summary ['1'] * 0 - $votes_summary ['0'] * - 5) / $votes_all );
	}
	$row ['avg_vote'] = $avg_vote;
	$row ['cvotes'] = $votes_all;
	$q = 'select sum(`in`) as tin, sum(`out`) as tout from hl_traffic where listing_id = ' . $row ['id'] . ' and date + interval ' . $settings ['traffic_count_days'] . ' day > now()';
	if (! ($ssth = mysql_query ( $q ))) { 
		exit ( mysql_error () );
	}
	$traffic = mysql_fetch_array ( $ssth );
	$row ['in'] = intval ( $traffic ['tin'] );
	$row ['out'] = intval ( $traffic ['tout'] );
	$row ['traffic_ratio'] = ($traffic ['tout'] == 0 ? '0.0' : sprintf ( '%.01f', $traffic ['tin'] / $traffic ['tout'] ));
	array_push ( $listings, $row );
}
?>
<script language="javascript">function editVotes(id) {  w = 400; h = 600;  t = (screen.height-h-30)/2;  l = (screen.width-w-30)/2;  window.open('?a=edit_votes&lid=' + id, 'edit_votes' + id, "top="+t+",left="+l+",width="+w+",height="+h+",resizable=1,scrollbars=1");}</script>
<form name="grps"><input type=hidden name=a value=new_listings>

<table cellspacing=1 cellpadding=2 border=0 width=100%>
	<tr>
		<td><b>Waiting Approval Listings: <?php echo $listing ['name']; echo "(" .$listing ['type']; ?>)</b>
		</td>
		<td width=1% nowrap> <select name="gid" class=inpts
			onchange="document.grps.submit()"> 
			<?php 
			foreach ( $listings as $grp ) {
			?>	
				<option value="<? echo $grp ['id'];?> "
				;
				<?php echo ($grp ['id'] == $listing ['id'] ? 'selected' : '');?>>
				<?php echo $grp ['name'];?>
				<?php echo($grp ['cnew'] )?>
				<?php echo ($grp ['status'] ? '' : ' (disabled)')?>
				</option> 
			<?php }?>
		</select> <input type="submit" value="GO" class=sbmt></td>
	</tr>
</table>
</form>
<table cellspacing=1 cellpadding=2 border=0 width=100%><?php 
if (0 < sizeof ( $listings )) {
	foreach ( $listings as $list ) { 
		$payments = preg_split ( '/,/', $list ['pay_systems'] );
?>
<tr>
		<td>
		<table cellspacing=0 cellpadding=1 border=0 width=100%>
			<tr>
				<td bgcolor=FFF9B3>
				<table cellspacing=0 cellpadding=2 border=0 bgcolor=#FFFFFF
					width=100%>
					<tr>
						<td valign=top width=33% nowrap><a
							href="<?php  echo $list ['url'];?>" target=_blank><b><?php echo $list ['name'];?> </b></a>			 	<?php echo ($list ['confirm'] ? '<br><font color="red"><b>Not Confirmed</b></font>' : '');?>
		 	 <br>       Paying Status: <?php echo getPayingStatus($list ['hyip_status']);?>			 <br />
						<a href="mailto:'<?php echo $list ['email']?>'">Contact</a></td>
						<td valign=top width=33% nowrap><b> <?php echo $list ['percents'];?> </b>
						<br>
						Minimal Spend: <b> <?php  echo $list ['min_spend'];?> </b> <br>
						Maximal Spend: <b> <?php echo ($list ['max_spend'] ? $list ['max_spend'] : 'No Limit');?> </b>
						<br>
						Referral: <b> <?php echo ($list ['referral'] ? $list ['referral'] : 'No');?> </b>
						<br>
						Withdrawal: <b><?php echo getWithDrawalType($list ['withdrawal_type']);?> </b>
						<br>
						</td>
						<td valign=top width=33% nowrap>			       Our Rating: <?php echo getOurRatting($list ['rating']);?> 
			       <br>			       Votes: <b><?php echo $list ['avg_vote'];?> - <?php echo $list ['cvotes'];?> votes</b>
			       <br>			       <a href="javascript:editVotes('<?php  echo $list ['id'];?>')">Manage Votes</a>
			       <br>							<?php if ((((($list ['support_email'] or $list ['support_form']) or $list ['support_forum']) or $list ['support_phone']) or $list ['support_aim'])) {?>
								Support: 								<?php if ($list ['support_email']) {?>
									<a href="mailto:' <?php echo $list ['support_email'];?>'"><img src="images/smail.gif" border=0 alt="Support E-Mail" title="Support E-Mail" align=absmiddle></a>
								<?php }?>
								<?php if ($list ['support_form']) {?>
									<a href="' <?php echo $list ['support_form'];?>'" target=_blank><img src="images/sform.gif" border=0 alt="Support Form" title="Support Form" align=absmiddle></a>
								<?php }?>
								<?php if ($list ['support_forum']) {?>
									<a href="'<?php echo $list ['support_forum'];?>'" target=_blank><img src="images/sforum.gif" border=0 alt="Support Forum" title="Support Forum" align=absmiddle></a>
								<?php }?>
								<?php if ($list ['support_phone']) {?>
									<img src="images/sphone.gif" border=0 alt="Phone: <?php echo $list ['support_phone'];?>"
									 title="Phone: <?php echo $list ['support_phone'];?>" align=absmiddle>
								<?php }?>
								<?php if ($list ['support_aim']) {?>
									<img src="images/smsn.gif" border=0 alt=" <?php echo $list ['support_aim'];?>"
									title=" <?php echo $list ['support_aim'];?>" align=absmiddle />
								<?php }?>
								<br>							<?php }?>
						Added: <b> <?php echo $list ['added'];?> </b>
						<br>
						Started: <b> <?php echo $list ['started'];?> </b>
						<br>				    In: <b> <?php echo $list ['in'];?></b> 
				    Out: <b> <?php echo $list ['out'];?> </b> 
				    Ratio: <b><?php echo $list ['traffic_ratio']; ?></b>				    <br>    			</td>     <td valign=top width=33%>       <a href="?a=approve_listing&lid=<?php  echo $list ['id'];?>&gid=<?php echo $frm ['gid'];?>&p=<?php echo $frm ['p'];?>&return=new_listings">[approve]</a>
       <br>       <a href="?a=decline_listing&lid=<?php echo $list ['id'];?>&gid=<?php echo $frm ['gid'];?>&p=<?php echo $frm ['p'];?>&return=new_listings">[decline]</a>
       <br>       <a href="?a=edit_listing&lid=<?php echo $list ['id'];?>&gid=<?php echo $frm ['gid'];?>&p=<?php echo $frm ['p'];?>&return=new_listings">[edit]</a><br>       <a href="?a=delete_listing&lid=<?php echo $list ['id'];?>&gid=<?php echo $frm ['gid'];?>&p=<?php echo $frm ['p'];?>
						&return=new_listings" onclick="return confirm('Do you really want to delete this listing?')">[delete]</a>
			 <br>       <br>     </td>    </tr>    <tr>      <td colspan=4 nowrap>
		<?php foreach ( $payments as $pay ) {?>
			<img src="images/<?php  echo $pay;?>.gif" align=absmiddle alt="<?php echo $pay;?> title="<?php echo $pay;?>">
		
		<?php }?>
		</td>    </tr>   </table>  </td></tr></table> </td></tr><?php 
	}
} else {?>
<tr> <td bgcolor=FFF9B3 colspan=4>No Listings found.</td></tr><?php }?>
</table><br><center>
<?php if (1 < $colpages) { 
	if (1 < $page) {
		echo ' <a href="?a=listings&gid=';
		echo $frm ['gid'];
		echo '&p=';
		echo $page - 1;
		echo '">&lt;&lt;</a> ';
	}
	for($i = 1; $i <= $colpages; ++ $i) {
		if ($i == $page) {
			echo ' <b>';
			echo $i;
			echo '</b> ';
			continue;
		} else {
			echo ' <a href="?a=listings&gid=';
			echo $frm ['gid'];
			echo '&p=';
			echo $i;
			echo '">';
			echo $i;
			echo '</a> ';
			continue;
		}
	}
	if ($page < $colpages) {
		echo ' <a href="?a=listings&gid=';
		echo $frm ['gid'];
		echo '&p=';
		echo $page + 1;
		echo '">&gt;&gt;</a> ';
	}
}?>
</center>
